<?php

/**
 * 购物车
 */
namespace app\order\api;

class CartApi extends \app\member\api\MemberApi {

    /**
     * 购物车列表
     */
    public function index() {
        $list = target('order/Cart', 'service')->getList($this->userId);
        $info = target('order/Cart', 'service')->getCart($this->userId);
        $this->success('ok', [
            'list' => $list,
            'info' => $info
        ]);
    }

    /**
     * 提交购物车
     */
    public function submit() {
        if (!isPost()) {
            $addId = $this->data['add_id'];
            $addInfo = target('order/Address', 'service')->getAddress($this->userId, $addId);

            $list = target('order/Cart', 'service')->getList($this->userId);

            if(empty($list)) {
                $this->error404();
            }

            $orderData = target('order/Order', 'service')->splitOrder($addInfo['province'], $list);

            $deliveryPrice = 0;
            foreach ($orderData as $vo) {
                $deliveryPrice += $vo['delivery_price'];
            }
            $orderPrice = $info['total'];
            $this->success('ok', [
                'list' => $orderData,
                'deliveryPrice' => $deliveryPrice,
                'orderPrice' => $orderPrice,
                'addInfo' => $addInfo,
            ]);
        } else {
            $codStatus = $this->data['cod_status'];
            $target = target('order/Order', 'service');
            $orderNos = $target->addOrder($this->userId, '', $this->data['add_id'], $codStatus, $this->data['remark']);
            if (!$orderNos) {
                $this->error($target->getError());
            }
            target('order/Cart', 'service')->clear($this->userId);

            $orderList = target('order/Order')->loadList([
                '_sql' => 'order_no in(' . implode(',', $orderNos) . ')'
            ]);


            $deliveryPrice = 0;
            $orderPrice = 0;
            foreach ($orderList as $vo) {
                $deliveryPrice += $vo['delivery_price'];
                $orderPrice += $vo['order_price'];
            }

            $accountInfo = target('member/PayAccount')->getWhereInfo([
                'A.user_id' => $this->userId
            ]);

            if (!$codStatus) {
                $this->success('订单提交成功,请选择付款方式!', ['order_no' => implode('|', $orderNos), 'order_price' => $orderPrice, 'delivery_price' => $deliveryPrice, 'user_money' => $accountInfo['money']]);
            } else {
                $this->success('订单提交成功,请耐心等待发货!');
            }
        }
    }


    public function num() {
        if (empty($this->data['rowid'])) {
            $this->error('商品信息获取失败,请重试!');
        }
        if (!$this->data['qty']) {
            $this->error('商品数量不能为0!');
        }
        $cartData = [];
        $cartData['rowid'] = $this->data['rowid'];
        $cartData['qty'] = $this->data['qty'];
        if (!target('order/Cart', 'service')->update($this->userId, $cartData)) {
            $this->error(target('order/Cart', 'service')->getError());
        }
        $list = target('order/Cart', 'service')->getList($this->userId);
        $info = target('order/Cart', 'service')->getCart($this->userId);
        $this->success('ok', [
            'row' => $list[$this->data['rowid']],
            'info' => $info
        ]);
    }

    public function del() {
        if (empty($this->data['rowid'])) {
            $this->error('商品信息获取失败,请重试!');
        }
        if (!target('order/Cart', 'service')->del($this->userId, explode(',', $this->data['rowid']))) {
            $this->error(target('order/Cart', 'service')->getError());
        }
        $info = target('order/Cart', 'service')->getCart($this->userId);
        $this->success('ok', [
            'info' => $info
        ]);
    }

}